// Copyright (c) 2023 CHANGLEI. All rights reserved.

import 'package:flutter/material.dart';
import 'package:tverify/src/extensions.dart';
import 'package:tverify/src/ui_decorator.dart';

/// 文本对齐方式
enum TextAlignment {
  /// 居左
  left,

  /// 居中
  center,

  /// 居右
  right,
}

/// Created by changlei on 2023/8/15.
///
/// UIView
final class UIView {
  /// UIView
  const UIView({
    required this.id,
    this.left = 0,
    this.top = 0,
    this.width = 0,
    this.height = 0,
    this.padding = EdgeInsets.zero,
    this.backgroundColor,
    this.background,
    this.onPressed,
  });

  /// ID
  final String id;

  /// 左边距，屏幕左边缘开始计算
  final double left;

  /// 上边距，导航栏底部开始计算
  final double top;

  /// 宽
  final double width;

  /// 高
  final double height;

  /// 内边距
  final EdgeInsets padding;

  /// 背景颜色
  final Color? backgroundColor;

  /// 背景图片
  final UIImage? background;

  /// 点击事件
  final VoidCallback? onPressed;

  /// toMap
  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      'type': runtimeType.toString(),
      'id': id,
      'left': left,
      'top': top,
      'width': width,
      'height': height,
      'padding': padding.toMap(),
      'backgroundColor': backgroundColor?.toMap(),
      'background': background?.toMap(),
      'isClickEnable': onPressed != null,
    };
  }
}

/// TextView
final class UITextView extends UIView {
  /// TextView
  const UITextView({
    required super.id,
    super.left,
    super.top,
    super.width,
    super.height,
    super.padding,
    super.backgroundColor,
    super.background,
    super.onPressed,
    this.title = '',
    this.titleFont = const UIFont(
      fontSize: 13,
      fontWeight: FontWeight.normal,
    ),
    this.titleColor = Colors.black,
    this.textAlignment,
    this.lines = 1,
    this.isSingleLine = true,
    this.isShowUnderline = false,
  });

  /// 文本
  final String title;

  /// 字体大小
  final UIFont titleFont;

  /// 字体颜色
  final Color titleColor;

  /// 文本对齐方式
  final TextAlignment? textAlignment;

  /// textView 行数，
  final int lines;

  /// textView 是否单行显示，默认：单行，iOS 端无效
  /// 若 isSingleLine = false 时，iOS 端 lines 设置失效，会自适应内容高度，最大高度为设置的 height
  final bool isSingleLine;

  /// 是否显示下划线，默认：不显示
  final bool isShowUnderline;

  @override
  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      ...super.toMap(),
      'title': title,
      'titleFont': titleFont.toMap(),
      'titleColor': titleColor.toMap(),
      'textAlignment': textAlignment?.index,
      'backgroundColor': backgroundColor?.toMap(),
      'lines': lines,
      'isSingleLine': isSingleLine,
      'isShowUnderline': isShowUnderline,
    };
  }
}

/// Button
final class UIButton extends UITextView {
  /// Button
  const UIButton({
    required super.id,
    super.left,
    super.top,
    super.width,
    super.height,
    super.backgroundColor,
    super.background,
    super.onPressed,
    super.title,
    super.titleFont,
    super.titleColor,
    super.textAlignment,
    super.lines,
    super.isSingleLine,
    super.isShowUnderline,
    this.normalImage,
    this.pressedImage,
  });

  /// 按钮默认背景
  final UIImage? normalImage;

  /// 按钮按下背景
  final UIImage? pressedImage;

  @override
  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      ...super.toMap(),
      'btnNormalImage': normalImage?.toMap(),
      'btnPressedImage': pressedImage?.toMap(),
    };
  }
}

/// ImageView
final class UIImageView extends UIView {
  /// ImageView
  const UIImageView({
    required super.id,
    super.left,
    super.top,
    super.width,
    super.height,
    super.padding,
    super.backgroundColor,
    super.background,
    super.onPressed,
    this.src,
    this.fit = BoxFit.cover,
  });

  /// 图片
  final UIImage? src;

  /// 缩放方式
  final BoxFit fit;

  @override
  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      ...super.toMap(),
      'src': src?.toMap(),
      'fit': fit.index,
    };
  }
}

/// ViewGroup
final class UIViewGroup extends UIView {
  /// ViewGroup
  const UIViewGroup({
    required super.id,
    super.left,
    super.top,
    super.width,
    super.height,
    super.padding,
    super.backgroundColor,
    super.background,
    super.onPressed,
    this.children,
  });

  /// 图片
  final List<UIView>? children;

  @override
  Map<String, dynamic> toMap() {
    return <String, dynamic>{
      ...super.toMap(),
      'children': children?.map((e) => e.toMap()).toList(),
    };
  }
}
